package io.renren.modules.substation.controller;

import java.util.*;

import io.renren.modules.substation.entity.TInfoDevInfoEntity;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.substation.entity.TInfoEfencePointEntity;
import io.renren.modules.substation.service.TInfoEfencePointService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 围柱坐标表
 *
 * @author yzx
 * @email sunlightcs@gmail.com
 * @date 2020-07-16 01:02:14
 */
@RestController
@RequestMapping("substation/tinfoefencepoint")
public class TInfoEfencePointController extends AbstractController {
    @Autowired
    private TInfoEfencePointService tInfoEfencePointService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("substation:tinfoefencepoint:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = tInfoEfencePointService.queryPage(params);

        return R.ok().put("page", page);
    }

//    @RequestMapping("/efencepointlist")
//    public R devlist(){
//
//        List<TInfoEfencePointEntity> dev_List= tInfoEfencePointService.list();
//
//        return R.ok().put("list", dev_List);
//    }

    /**
     * 信息
     */
    @RequestMapping("/info/{devCode}&{id2}")
    @RequiresPermissions("substation:tinfoefencepoint:info")
    public R info(@PathVariable("devCode") String devCode,@PathVariable("id2") String id2){

        logger.debug("devCode : " + devCode + " id2 :" + id2);
//		TInfoEfencePointEntity tInfoEfencePoint = tInfoEfencePointService.getById(1);
        TInfoEfencePointEntity tInfoEfencePoint = new TInfoEfencePointEntity();
        tInfoEfencePoint.setPostCode(id2);
        tInfoEfencePoint.setDevCode(devCode);
        if(id2 != null && devCode != null) {
            tInfoEfencePoint = tInfoEfencePointService.getByPostCodeAndDevCode(tInfoEfencePoint);
        }
        return R.ok().put("tInfoEfencePoint", tInfoEfencePoint);
    }

    /**
     * 信息
     */
    @RequestMapping("/listpostall/{devCode}")
//    @RequiresPermissions("substation:tinfoefencepoint:listpostall")
    public R info(@PathVariable("devCode") String devCode){

        logger.debug("devCode : " + devCode);
//		TInfoEfencePointEntity tInfoEfencePoint = tInfoEfencePointService.getById(1);
//        TInfoEfencePointEntity tInfoEfencePoint = new TInfoEfencePointEntity();
//        tInfoEfencePoint.setPostCode(id2);
//        tInfoEfencePoint.setDevCode(devCode);
        List<TInfoEfencePointEntity> list = null;
        if(devCode != null) {
            list = tInfoEfencePointService.getByDevCode(devCode);
        }
        return R.ok().put("list", list);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("substation:tinfoefencepoint:save")
    public R save(@RequestBody TInfoEfencePointEntity tInfoEfencePoint){

        logger.debug("tInfoEfencePoint : "+tInfoEfencePoint);
        tInfoEfencePoint.setLastOpr(getUserId());
        tInfoEfencePoint.setLastTime(new Date());
        if(tInfoEfencePoint.getDevCode() != null && tInfoEfencePoint.getPostCode() != null) {
            int updsum = tInfoEfencePointService.updateByPostCodeAndDevCode(tInfoEfencePoint);
            if (updsum <= 0) {
                tInfoEfencePoint.setCreateOpr(getUserId());
                tInfoEfencePoint.setCreateTime(new Date());
                boolean saveflag = tInfoEfencePointService.save(tInfoEfencePoint);
                if (!saveflag) {
                    logger.debug("tInfoEfencePointService : fail");
                }
            }
        }


        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("substation:tinfoefencepoint:update")
    public R update(@RequestBody TInfoEfencePointEntity tInfoEfencePoint){
//		tInfoEfencePointService.updateById(tInfoEfencePoint);
        logger.debug("tInfoEfencePoint : "+tInfoEfencePoint);
        tInfoEfencePoint.setLastOpr(getUserId());
        tInfoEfencePoint.setLastTime(new Date());
        tInfoEfencePointService.updateByPostCodeAndDevCode(tInfoEfencePoint);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("substation:tinfoefencepoint:delete")
    public R delete(@RequestBody Long[] efencePointIds){
		tInfoEfencePointService.removeByIds(Arrays.asList(efencePointIds));

        return R.ok();
    }

}
